<template>
    <!--  :class="[store.isDark ? 'editor-area_dark' : 'editor-area_bright']" -->

    <!-- 主编辑器区域 -->
    <div :class="[store.isDark ? 'editor-area_dark' : 'editor-area_bright']">
        <!-- 标签tab区域 -->
        <div class="editor-tabs">
            <EditorTab @changeTab="switchTab" v-for="item in tabList" :editor-tab="item" :current-tab="currentTab">
            </EditorTab>
        </div>
        <!-- 编辑区域 -->
        <!-- <div class="editor-content">
            <div class="code-line" v-for="(lineCode, index) in lineCodes">
                <span class="line-number">{{ index + 1 }}</span>
                <span class="code-keyword">{{ lineCode }}</span>
            </div>
        </div> -->
        <div class="editor-content">
            <!-- <el-mention class="el-mention-class" v-model="value" type="textarea" :options="options" placeholder="Please input" /> -->
            <textarea class="textarea-class" v-model="lineCodes"></textarea>
        </div>
    </div>
</template>

<script setup>
import { ref, watch } from "vue";
import { useStore } from '../store/store'
import { storeToRefs } from "pinia";
import EditorTab from "./editortab/EditorTab.vue";
import { invoke } from "@tauri-apps/api/core";
import { inject } from 'vue'

import useCodeRef from "../customref/useCodeRef";
const toast = inject('toast')

const store = useStore()
//使用自定义ref，防抖
const lineCodes = ref('')
//wath，发生改变就写入文件，自动保存文件，2秒进行更新写入文件
const code = useCodeRef('', 1000)

function read_file(path) {
    const content = invoke('read_file', { 'path': path })
    return content
}


watch(lineCodes, async (newValue, oldValue) => {
    // console.log("lineCodes 改变:",newValue);
    code.value = newValue
})


watch(code, async (newValue, oldValue) => {
    //写入文件
    // console.log("newValue:", newValue);
    const curr = store.currentFile;
    // console.log(curr);
    if (curr !== '' && curr !== null) {
        try {
            await invoke('write_file', { 'path': curr, 'content': newValue })
        } catch (error) {
            toast("写入异常：" + error, "error")
        }
    }
})

watch(() => {
    return store.currentFile
}, async (newValue, oldValue) => {
    //读取新的文件内容
    // console.log('store的currentFile newValue:', newValue, 'oldValue:', oldValue);
    if (newValue == '') {
        //说明没有元素了
        lineCodes.value = '当前记录列表为空了,请添加文件'
        return
    }
    const content = await read_file(newValue)
    // console.log('content', content);

    //对内容进行渲染
    lineCodes.value = content

    // toast("读取成功!!!", "success")
}, {
    immediate: true
})


const { fileList } = storeToRefs(store)

//tabList
const tabList = fileList
//默认打开一个readme.txt
let currentTab = ref(fileList.value[0])

function switchTab(item) {
    currentTab.value = item
}

</script>


<style scoped>
.textarea-class {
    width: 100%;
    height: 90%;
    font-size: medium;
    padding: 20px;
    background-color: #242429;
    color: #c586c0;
    border: none;
    outline: none;
    border-radius: 10px;
}

/* 整个滚动条 */
textarea::-webkit-scrollbar {
    width: 12px;
    /* 垂直滚动条宽度 */
    height: 12px;
    /* 水平滚动条高度（可选） */
}

/* 滚动条轨道（背景） */
textarea::-webkit-scrollbar-track {
    background: #37383f;
    /* 轨道背景色 */
    border-radius: 10px;
    /* 圆角（可选） */
}

/* 滚动条滑块（可拖动部分） */
textarea::-webkit-scrollbar-thumb {
    background: #2b2c33;
    /* 滑块颜色 */
    border-radius: 10px;
    /* 圆角 */
    border: 2px solid #4e4949;
    /* 边框（可选） */
}

/* 滑块悬停状态 */
textarea::-webkit-scrollbar-thumb:hover {
    background: #353232;
    /* 悬停时颜色 */
}

/* 编辑器区域 */
.editor-area_dark {
    color: #d4d4d4;
    flex: 1;
    display: flex;
    flex-direction: column;
    /* color: #c586c0; */
    background-color: #1e1f24;
}


.editor-area_bright {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: #413838;
}

.editor-tabs {
    height: 36px;
    background-color: #303138;
    display: flex;
    border-bottom: 1px solid #181a1f;
    padding: 0 10px;
}


.editor-content {
    flex: 1;
    padding: 20px;
    font-family: 'Consolas', monospace;
    font-size: 14px;
    line-height: 1.8;
    overflow: auto;
}

.code-line {
    display: flex;
}

.line-number {
    width: 40px;
    color: #5a5d68;
    user-select: none;
}

.code-keyword {
    color: #c586c0;
}

.code-string {
    color: #ce9178;
}

.code-comment {
    color: #6a9955;
}

.code-function {
    color: #dcdcaa;
}

/* 状态栏 */
.status-bar {
    height: 26px;
    background-color: #0078d4;
    display: flex;
    align-items: center;
    padding: 0 15px;
    font-size: 12px;
}

.status-item {
    margin-right: 20px;
}

/* 代码示例区域 */
.code-container {
    padding: 30px;
    background: #25262c;
    margin-top: 30px;
    border-radius: 10px;
}

.code-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
}

.code-title {
    font-size: 18px;
    font-weight: 600;
    color: #4d78cc;
}

.code-description {
    color: #a0a0b0;
    margin-bottom: 25px;
    line-height: 1.6;
}

pre {
    background: #1e1f24;
    padding: 20px;
    border-radius: 8px;
    overflow: auto;
    font-family: 'Consolas', monospace;
    line-height: 1.5;
}


.buttons {
    display: flex;
    gap: 15px;
    margin-top: 30px;
    justify-content: center;
}

.btn {
    padding: 12px 25px;
    background: #4d78cc;
    color: white;
    border: none;
    border-radius: 8px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 8px;
}

.btn:hover {
    background: #5a87d9;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(77, 120, 204, 0.4);
}

.btn:active {
    transform: translateY(0);
}

.btn-outline {
    background: transparent;
    border: 1px solid #4d78cc;
    color: #4d78cc;
}

.btn-outline:hover {
    background: rgba(77, 120, 204, 0.1);
}

@media (max-width: 768px) {
    .title_class {
        flex-direction: column;
        height: auto;
        padding: 15px;
        gap: 15px;
    }

    .app-title {
        width: 100%;
        justify-content: center;
    }

    .menu-buttons {
        width: 100%;
        justify-content: center;
    }

    .main-content {
        flex-direction: column;
    }

    .file-explorer {
        width: 100%;
    }
}
</style>
